package com.owen.he.jee.service.impl;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.owen.he.jee.model.User;
import com.owen.he.jee.model.sms.SmsProvider;
import com.owen.he.jee.service.exception.Assert;
import com.owen.he.jee.service.exception.ServiceException;
import com.owen.he.jee.service.subService.SmsSender;
import com.owen.he.jee.utils.RegExpUtil;
import com.owen.he.jee.vo.ReturnData;

@Service
public class SmsSenderServiceImpl {
	
	@Value("${sms.username.lx198}")
	private String username;
	@Value("${sms.password.lx198}")
	private String password;
	@Value("${sms.url.lx198}")
	private String url;
	
	private @Resource SmsSender smsSender;
	
	/**
	 * @param content
	 * @param smsProvider
	 * @param mobilePhones
	 * @return
	 * @author owen.he
	 */
	public void validSmsMessage(String content, Integer maxLength, String mobilePhones) {
		try {
			//校验内容
			content = RegExpUtil.htmlSpecialCharacterRemover(content);
			Assert.isTrue(RegExpUtil.vaildCharacterMaxLength(content, maxLength), "发送的内容过长！！");
			
			//校验电话号码
			if (mobilePhones.indexOf(",") > 0) {
				String[] phones = mobilePhones.split(",");
				for (int i = 0; i < phones.length; i++) {
					Assert.isTrue(RegExpUtil.IsHandset(phones[i]), "电话号码有误！！！");
				}
			}else {
				Assert.isTrue(RegExpUtil.IsHandset(mobilePhones), "电话号码有误！！！");
			}
		} catch (Exception e) {
			throw new ServiceException("短信信息处理出错！！请检查");
		}
	}

	public SmsProvider getSmsProvider(String smsProvider) {
		//查询数据库
		return null;
	}
	
	
	public void  saveSms(String content, String smsProvider, String mobilePhones, ReturnData sendData, User user) {
		
	}
	
	public void smsSneder(String content, String smsProvider, String mobilePhones, User user) {
		try {
			SmsProvider pro = getSmsProvider(smsProvider);
			Assert.isNull(pro, "短信平台不存在！！！");
			validSmsMessage(content, pro.getMaxLength(), mobilePhones);
			//校验电话号码
			if (mobilePhones.indexOf(",") > 0) {
				String[] phones = mobilePhones.split(",");
				for (int i = 0; i < phones.length; i++) {
					ReturnData sendData =  smsSender.sendLx198(phones[i], content, pro);
					saveSms(content, smsProvider, mobilePhones, sendData, user);
				}
			}else {
				ReturnData sendData = smsSender.sendLx198(mobilePhones, content, pro);
				saveSms(content, smsProvider, mobilePhones, sendData, user);
			}
			
		} catch (Exception e) {
			throw new ServiceException("短信信息处理出错！！");
		}
		
	}
	
	
	
}
